//
//  BYHomeTableViewController.m
//  School+
//
//  Created by Sean Sebastian on 12/3/15.
//  Copyright © 2015 Sean Sebastian. All rights reserved.
//

#import "BYHomeTableViewController.h"

#import "UserAccount.h"
#import "Blog.h"
#import "NSString+BYDateAndString.h"
#import "MJExtension.h"
#import "BYLocationImageViewController.h"
#import "masonry.h"

#import "BYBlogCell.h"

@interface BYHomeTableViewController () <BYBlogCellDelegate>

@property (nonatomic , strong) NSMutableArray *blogArr;
@property (nonatomic , assign) CGFloat *h;


@property (nonatomic , weak) UIImageView *locationView;
@property (nonatomic , weak) UIButton *closeBtn;

@end

@implementation BYHomeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"主页";

    
    if (![UserAccount sharedUserAccount].isSignIn) {
        return;
    }
    NSLog(@"已登陆，获取用户信息");
    [self loadBlog];
    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.blogArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BYBlogCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[BYBlogCell alloc] init];
    }
    
    cell.delegate = self;
    cell.blog = self.blogArr[indexPath.row];
    

    
    return cell;
}
- (void)locationClick:(Blog *)blog
{
    BYLocationImageViewController *vc =[[BYLocationImageViewController alloc] init];
    vc.blog = blog;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)loadBlog
{
    NSMutableArray *blogArr = [[NSMutableArray alloc] init];
    
    NSURL *url = [NSURL URLWithString:@"http://localhost/SchoolPlus/loadBlog.php"];
    NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@",[UserAccount sharedUserAccount].username,[UserAccount sharedUserAccount].password];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (data == nil) {
        NSLog(@"send request faild : %@" , error);
    }
    NSArray *result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
    for (NSDictionary *dict in result) {
        Blog *blog = [Blog mj_objectWithKeyValues:dict];
        blog.blogTime = [NSString dateFromString:dict[@"blogTime"]];
        [blogArr addObject:blog];

    }
    
    self.blogArr = blogArr;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Blog *blog = self.blogArr[indexPath.row];

    
    return blog.cellHeight;
}

- (NSMutableArray *)blogArr
{
    if (_blogArr == nil) {
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        _blogArr = arr;
        return arr;
    }
    return _blogArr;
}


- (UIImageView *)locationView
{
    if (_locationView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.alpha = 0.8f;
        _locationView = imageView;
        return imageView;
    }
    return _locationView;
}

- (UIButton *)closeBtn
{
    if (_closeBtn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [btn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _closeBtn = btn ;
        return btn;
    }
    return _closeBtn;
}

- (void)closeBtnClick
{
    self.locationView.hidden = YES;
}

@end
